package com.lebang.service.sys.other;

import com.alibaba.fastjson.JSON;
import com.lebang.component.RedisClient;
import com.lebang.domain.AppResoure;
import com.lebang.entity.RequestHeaders;
import com.lebang.entity.member.LbbMember;
import com.lebang.example.AppResoureExample;
import com.lebang.mapper.AppResoureMapper;
import com.lebang.mapper.LbbMemberMapper;
import com.lebang.mapper.TaskRewardsRecordMapper;
import com.lebang.service.BaseService;
import com.lebang.service.DownAppService;
import com.lebangweb.common.TextUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import static com.lebang.controller.api.member.MemberApi.MEMBER_DATE;

/**
 * 新菜单
 *
 * @Author hwq
 * @Date 2020-04-01
 */
@Service
public class AppResoureService extends BaseService<AppResoure, Integer, AppResoureExample> {
    @Autowired
    private AppResoureMapper appResoureMapper;
    private static final String KEY = "AppResoure";
    @Autowired
    private RedisClient redisClient;
    @Autowired
    private LbbMemberMapper memberMapper;
    @Autowired
    private TaskRewardsRecordMapper taskRewardsRecordMapper;
    @Autowired
    private DownAppService downAppService;

    public List<AppResoure> selectListByUserInfo(RequestHeaders requestHeaders, int openAddress) {
        boolean xsrw = false;
        LbbMember lbbMember = memberMapper.getMemberByMemberId(requestHeaders.getUserid());
        if (lbbMember != null) {
            // 以前创建的为老用户
            if (lbbMember.getCreateTime().before(MEMBER_DATE)) {
                xsrw = true;
            } else {
                //完成所有新手任务则隐藏新手任务入口
                xsrw = taskRewardsRecordMapper.getRecordByMemberIdAndRewardsId(requestHeaders.getUserid(), 1) != null;
            }
        }
        boolean mrbz = downAppService.checkDoRecord(requestHeaders);
        int version = Integer.parseInt(requestHeaders.getVersion());
        List<AppResoure> list;
        Optional<String> str = redisClient.Optionalhget(KEY, requestHeaders.getAppStore() + ":" + version + ":" + openAddress);
        if (!str.isPresent()) {
            list = appResoureMapper.selectListByUserInfo(requestHeaders.getAppStore(), version, openAddress);
            if (list.size() != 0) {
                redisClient.hset(KEY, requestHeaders.getAppStore() + ":" + version + ":" + openAddress, JSON.toJSONString(list));
            }
        } else {
            list = JSON.parseArray(str.get(), AppResoure.class);

        }
        // 过滤新手任务
        if (list != null && requestHeaders.getUserid() != null && !TextUtil.isEmpty(requestHeaders.getUserid())) {
            boolean finalXsrw = xsrw;
            list.forEach(s -> {
                if (mrbz) {
                    s.setList(s.getList().stream().filter(appMain -> !"每日必赚".equals(appMain.getTitle())).collect(Collectors.toList()));
                }
                if (finalXsrw) {
                    s.setList(s.getList().stream().filter(appMain -> !"新手任务".equals(appMain.getTitle())).collect(Collectors.toList()));
                }
            });
        }
        return list;
    }

    public void deleteRedis() {
        redisClient.del(KEY);
    }
}